import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*
 * GUI
*/

public class GUI_SC extends JFrame implements ActionListener {
 
private static final long serialVersionUID = 1L;
    // will first hold "Username:", later on "Enter message"
    private JLabel label;
    // to hold the Username and later on the messages
    private JTextField tf;
    // to hold the server address
    private JTextField tfServer;
    // to Logout and get the list of the users
    private JButton login, logout;
    // if it is for connection
    private boolean connected;
    // for the chat room
    private JTextArea ta;
    // the Client object

    private Client client;
    
    private String defaultHost;
 
    // Constructor connection receiving a socket number
    GUI_SC(String host) {
 
        super("Chat Client");
        defaultHost = host;
         
        // The NorthPanel with:
        JPanel northPanel = new JPanel(new GridLayout(3,1));
        // the server name
        JPanel serverName = new JPanel(new GridLayout(1,5, 1, 3));
        // the JTextField with default value for server address
        tfServer = new JTextField(host);
         
        serverName.add(new JLabel("Server Address:  "));
        serverName.add(tfServer);
        serverName.add(new JLabel(""));
        // adds the Server to the GUI
        northPanel.add(serverName);
 
        // the Label and the TextField
        label = new JLabel("Enter your username below", SwingConstants.CENTER);
        northPanel.add(label);
        tf = new JTextField("Anonymous");
        tf.setBackground(Color.WHITE);
        northPanel.add(tf);
        add(northPanel, BorderLayout.NORTH);
 
        // The CenterPanel which is the chat room
        ta = new JTextArea("Bienvenue au Chat \n", 80, 80);
        JPanel centerPanel = new JPanel(new GridLayout(1,1));
        centerPanel.add(new JScrollPane(ta));
        ta.setEditable(false);
        add(centerPanel, BorderLayout.CENTER);
 
        // the 2 buttons
        login = new JButton("Login");
        login.addActionListener(this);
        logout = new JButton("Logout");
        logout.addActionListener(this);
        logout.setEnabled(false);       // you have to login before being able to logout
        
	JPanel southPanel = new JPanel();
        southPanel.add(login);
        southPanel.add(logout);
        add(southPanel, BorderLayout.SOUTH);
 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 600);
        setVisible(true);
        tf.requestFocus();
 
    }
 
    // called by the Client to append text in the TextArea
    void append(String str) {
        ta.append(str);
        ta.setCaretPosition(ta.getText().length() - 1);
    }
    
    // called by the GUI is the connection failed
    // we reset our buttons, label, textfield
    void connectionFailed() {
        login.setEnabled(true);
        logout.setEnabled(false);
        label.setText("Enter your username below");
        tf.setText("Anonymous");
        // reset host name as a construction time
        tfServer.setText(defaultHost);
        // let the user change them
        tfServer.setEditable(false);
        // don't react to a <CR> after the username
        tf.removeActionListener(this);
        connected = false;
    }
         
    /*
    * Button or JTextField clicked
    */
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        // if it is the Logout button
        if(o == logout) {
            client.sendMessage(new ChatMessageImp(ChatMessageImp.LOGOUT, ""));
            return;
        }
         
        // ok it is coming from the JTextField
        if(connected) {
            // just have to send the message
            client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, tf.getText()));            
            tf.setText("");
            return;
        }
         
 
        if(o == login) {
            // ok it is a connection request
            String username = tf.getText().trim();
            // empty username ignore it
            if(username.length() == 0)
                return;
            // empty serverAddress ignore it
            String server = tfServer.getText().trim();
            if(server.length() == 0)
                return;
             
            // try creating a new Client with GUI
            client = new Client(server, username, this);
            // test if we can start the Client
            if(!client.start())
                return;
            tf.setText("");
            label.setText("Enter your message below");
            connected = true;
             
            // disable login button
            login.setEnabled(false);
            // enable the 2 buttons
            logout.setEnabled(true);
            // disable the Server and Port JTextField
            tfServer.setEditable(false);
            tfPort.setEditable(false);
            // Action listener for when the user enter a message
            tf.addActionListener(this);
        }
 
    }
 
    
    /*public static void main(String[] args) {
        new ClientGUI("localhost");
    }*/
 
}